<?php
include_once 'common/tools.php';

//启用一个带有ob_gzip压缩机的工作台
ob_start('ob_gzip');

$matchTime = $_GET["matchTime"];
@$matchStatus = $_GET["matchStatus"];
@$locale = $_GET["locale"];
$matchCategory = $_GET["matchCategory"];

$config = require 'conf/config.ini.php';      //加载公用config.ini.php
	
$conn = mysql_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"]);
mysql_select_db($config["DB_NAME"], $conn);
mysql_query("SET NAMES utf8");

$sql = "select m.matchId," .
		"	   m.matchName," .
		"	   m.matchTime," .
		"	   m.matchCategory," .
		"	   m.matchStatus," .
		"	   m.matchRecord," .
		"	   m.matchOptaId," .
		"	   m.matchEvent," .
		"	   m.visitTeam visitTeamName," .
		"	   IFNULL((select ";
		if(strpos($locale,"EN")){
$sql .= "	   name_en ";
		}else if(strpos($locale,"TW")||strpos($locale,"HK")){
$sql .= "	   name_tw ";
		}else{
$sql .= "	   name ";
		}
$sql .= "		 from (select name,name_en,name_tw from live_team_b UNION ALL select name,name_en,name_tw from live_team_f) t" .
		"		 where t.name = m.visitTeam";
$sql .= "	   ),m.visitTeam) visitTeam," .
		"	   m.visitTeamPic," .
		"	   m.visitTeamScore," .
		"	   m.homeTeam homeTeamName," .
		"	   IFNULL((select ";
		if(strpos($locale,"EN")){
$sql .= "	   name_en ";
		}else if(strpos($locale,"TW")||strpos($locale,"HK")){
$sql .= "	   name_tw ";
		}else{
$sql .= "	   name ";
		}
$sql .= "		 from (select name,name_en,name_tw from live_team_b UNION ALL select name,name_en,name_tw from live_team_f) t" .
		"		 where t.name = m.homeTeam";
$sql .= "	   ),m.homeTeam) homeTeam," .
		"	   m.homeTeamPic," .
		"	   m.homeTeamScore," .
"COUNT(DISTINCT CONCAT(w.userIp, w.userMac)) matchWatch " .
"FROM live_match m LEFT JOIN live_watch w ON m.matchId = w.matchId " .
"WHERE	 1=1 ";
if ($matchStatus != "" && iconv("gbk", "UTF-8", $matchStatus) != iconv("gbk", "UTF-8","全部") ) {
	$sql .= "and matchStatus = '" . $matchStatus . "' ";
}
if ($matchCategory != "0") {
	$sql .= "and matchCategory in (" . $matchCategory . ") ";
}
$sql .= "and 	m.matchTime >=  '" . $matchTime . "' ";
$sql .= "and	m.matchTime < date_add('".$matchTime."',INTERVAL 1 day) ";
$sql .= "GROUP BY m.matchId " .
"order by matchTime, matchCategory";

$matchList = mysql_query($sql, $conn);
while ($match = mysql_fetch_object($matchList)) {
	$matchArray[] = $match;
}

//echo '{"sql":"' . $sql . '","results":' . json_encode($matchArray) . '}';
echo '{"results":' . json_encode($matchArray) . '}';

//输出压缩成果
ob_end_flush();
?>
